package Client;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;

public class MainUI extends JDialog {

    /**登录成功的用户对象*/
    User user;

    /**退出*/
    JButton bExit;
    /**最小化*/
    JButton bMin;

    /**最小化到系统托盘*/
    PopupMenu pm;
    MenuItem mit;
    TrayIcon icon;
    SystemTray tray;

    /**联系人*/
    JButton bContact;
    /**消息*/
    JButton bChatList;
    /**搜索框*/
    JTextField searchText;
    /**搜索按钮*/
    JButton bSearch;
    /**添加好友标签*/
    JButton addFriend;
    /**联系人*/
    JLabel lContact;
    /**消息*/
    JLabel lChatList;

    /**联系人面板*/
    FriendPanel friendPanel;

    Color defaultBGColor = Color.LIGHT_GRAY;

    /**主面板*/
    private JPanel content;
    /**基本信息面板*/
    private JPanel topPanel;
    /**好友信息面板*/
    private JPanel mainPanel;
    /**底部工具栏面板*/
    private JPanel bottomPanel;

    /**标记（左上角） */
    private JLabel productInfo;
    /**头像*/
    private JLabel picture;
    /**签名*/
    private JLabel signature;
    /**昵称*/
    private JLabel nickName;

    private Container container = this.getContentPane();


    MainUI(User user) {
        setSize(300, 680);
        setLocationRelativeTo(null);
        setUndecorated(true);
        this.user = user;
        MainUISet();
        initTrayIcon();//使主界面最小化到系统托盘的方法
        setMainUIActionListener(new MainUIActionListener(this,this.user));//设置监听本界面的代理

        setVisible(true);
    }

    private void MainUISet() {

        Font font1 = new Font("黑体", Font.BOLD, 20);

        //主面板
        content = new JPanel();
        content.setLayout(null);
        getContentPane().add(content);
        container.add(content);
        content.setBackground(defaultBGColor);


        //顶部面板
        topPanel = new JPanel();
        content.add(topPanel);//add到content的上面
        topPanel.setLayout(null);
        topPanel.setOpaque(false);
        topPanel.setBounds(1, 1, 300, 142);

        productInfo = new JLabel(new ImageIcon("src/image/maintitle.png"));
        topPanel.add(productInfo);
        productInfo.setBounds(0, 0, 40, 40);


        bMin = new JButton("—");
        bMin.setForeground(Color.BLACK);
        topPanel.add(bMin);
        bMin.setBorder(null);
        bMin.setContentAreaFilled(false);
        bMin.setBounds(219, 0, 40, 30);

        bExit = new JButton("✕");
        bExit.setForeground(Color.BLACK);
        topPanel.add(bExit);
        bExit.setBorder(null);
        bExit.setContentAreaFilled(false);//设置透明，显示出背景图片
        bExit.setBounds(259, 0, 40, 30);


        nickName = new JLabel();
        nickName.setText(this.user.getUserId());
        nickName.setFont(font1);
        nickName.setForeground(Color.black);
        topPanel.add(nickName);
        nickName.setBounds(30, 50, 100, 30);

        searchText = new JTextField();
        searchText.setText("搜索：联系人");
        topPanel.add(searchText);
        searchText.setBounds(1, 111, 245, 30);
        searchText.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));

        bSearch = new JButton("搜索");
        topPanel.add(bSearch);
        bSearch.setContentAreaFilled(false);
        bSearch.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        bSearch.setBounds(245, 111, 54, 30);


        // 好友面板
        mainPanel = new JPanel();
        content.add(mainPanel);
        mainPanel.setLayout(null);
        mainPanel.setOpaque(false);
        mainPanel.setBounds(1, 142, 298, 500);
        mainPanel.setBackground(Color.WHITE);

        bContact = new JButton("联系人");
        bContact.setForeground(Color.BLACK);
        mainPanel.add(bContact);
        bContact.setContentAreaFilled(false);
        bContact.setBorder(BorderFactory.createEmptyBorder());
        bContact.setBounds(1, 1, 150, 30);

		/*bChatList=new JButton("消息");
		mainPanel.add(bChatList);
		bChatList.setBorder(null);
		bChatList.setBounds(150,1,147,30);*/

        friendPanel = new FriendPanel(this);
        mainPanel.add(friendPanel);
        friendPanel.setBounds(1, 33, 298, 460);

        //底部面板
        bottomPanel = new JPanel();
        content.add(bottomPanel);//add到content的上面
        bottomPanel.setLayout(null);
        bottomPanel.setOpaque(false);
        bottomPanel.setBounds(1, 637, 298, 40);

        addFriend=new JButton("➕");
        //addFriend=new JButton(new ImageIcon("src/image/add_friend.jpg"));
        bottomPanel.add(addFriend);
        addFriend.setContentAreaFilled(false);
        addFriend.setBounds(20, 0, 50, 40);
        addFriend.setBackground(Color.WHITE);

        Background mainBg = new Background();
        mainBg.setImage(this.getToolkit().getImage("src/image/mainUI_bg.jpg"));
        mainBg.setBounds(1, 1, 298, 678);
        content.add(mainBg);
        repaint();


    }

    private void initTrayIcon() {
        if (SystemTray.isSupported()) {
            try {
                tray = SystemTray.getSystemTray();
                icon = new TrayIcon(new ImageIcon("src/image/qq_icon.png").getImage());
                icon.setImageAutoSize(true); //自动适应大小
                pm = new PopupMenu();
                mit = new MenuItem("Exit");
                pm.add(mit);
                icon.setPopupMenu(pm);
                tray.add(icon);
            } catch (AWTException e) {
                e.printStackTrace();
            }
        }
    }

    private void setMainUIActionListener(MainUIActionListener mainUIActionListener) {
        bMin.addMouseListener(mainUIActionListener);
        bMin.addActionListener(mainUIActionListener);
        bExit.addActionListener(mainUIActionListener);
        bExit.addMouseListener(mainUIActionListener);
        searchText.addActionListener(mainUIActionListener);
        bSearch.addActionListener(mainUIActionListener);
        bSearch.addMouseListener(mainUIActionListener);
        bContact.addActionListener(mainUIActionListener);
        bContact.addMouseListener(mainUIActionListener);
        mit.addActionListener(mainUIActionListener);
        icon.addMouseListener(mainUIActionListener);
        addFriend.addMouseListener(mainUIActionListener);

    }


}


